/**
 * Created by ROGSTAR on 2016/11/7.
 */
require.config(window.jshead);
require(['jquery', 'pages', "layer",'show','navigator','radialIndicator'], function ($, pg,layer,sw) {

	var _hmt = _hmt || [];
	(function() {
		var hm = document.createElement("script");
		hm.src = "https://hm.baidu.com/hm.js?04655570a7f6ba29233f7e34e328d5bb";
			var s = document.getElementsByTagName("script")[0];
		s.parentNode.insertBefore(hm, s);
	})();
	console.log($('.right_part').find('.showBorder'))
    $('.right_part').find('.showBorder').eq(0).addClass("borderRight");
	$('.right_part').find('.showBorder').eq(0).addClass("borderBottom");
	$('.right_part').find('.showBorder').eq(1).addClass("borderBottom");
	$('.right_part').find('.showBorder').eq(2).addClass("borderRight");
	$('.right_part').find('.showBorder').eq(4).addClass("borderRight");
	$('.right_part').find('.showBorder').eq(4).addClass("borderBottom");
	$('.right_part').find('.showBorder').eq(5).addClass("borderBottom");
	$('.right_part').find('.showBorder').eq(6).addClass("borderRight");
//图片播放

	var aUlLi = $('#pic').find('li');
	var aOlLi = $('#dot').find('li');
	var iNow = 0;
	var timer = null;
	fnFade();

	aOlLi.click(function() {
		iNow = $(this).index();
		fnFade();
	});


	$('#dot').hover(function() {
		clearInterval(timer);
	}, function() {
		autoPlay();
	});
	function autoPlay() {
		if(timer){
			clearInterval(timer);
		}
		timer = setInterval(function() {
			iNow++;
			iNow %= aOlLi.length;
			fnFade();
		}, 5000);
	}

	autoPlay();

	function fnFade() {
		aUlLi.each(function (i) {
			if (i != iNow) {
				aUlLi.eq(i).fadeOut(1000).css('zIndex', 1);
				aOlLi.eq(i).removeClass('active');

			} else {
				aUlLi.eq(i).fadeIn(1000).css('zIndex', 2);
				aOlLi.eq(i).addClass('active');
			}
		});
	}
	//进度条
	radialIndicator.defaults.radius = 110;
	radialIndicator.defaults.barColor = "#ff5b5b";
	radialIndicator.defaults.barBgColor = "#e8e8e8";
	radialIndicator.defaults.barWidth = 10;
	radialIndicator.defaults.percentage = false;
	radialIndicator.defaults.displayNumber = false;

	if($('#newMember').length != 0) {
		$('#newMember').radialIndicator();
		var newMember = $('#newMember').data('radialIndicator');
		var newMembervalue = $('#newMember').data().value;
		newMember.value(newMembervalue);

	}
	if($('#app').length != 0) {
		$('#app').radialIndicator();
		var app = $('#app').data('radialIndicator');
		var appvalue = $('#app').data().value;
		app.value(appvalue);

	}

	if($('#trailer').length != 0) {
		$('#trailer').radialIndicator();
		var trailer = $('#trailer').data('radialIndicator');
		var trailervalue = $('#trailer').data().value;
		trailer.value(trailervalue);

	}
	if($('#chedaibao').length != 0) {
		$('#chedaibao').radialIndicator();
		var chedaibao = $('#chedaibao').data('radialIndicator');
		var chedaibaovalue = $('#chedaibao').data().value;
		chedaibao.value(chedaibaovalue);

	}
	if($('#xuechebao').length != 0) {
		$('#xuechebao').radialIndicator();
		var xuechebao = $('#xuechebao').data('radialIndicator');
		var xuechebaovalue = $('#xuechebao').data().value;
		xuechebao.value(xuechebaovalue);

	}
	if($('#cheshangbao').length != 0) {
		$('#cheshangbao').radialIndicator();
		var cheshangbao = $('#cheshangbao').data('radialIndicator');
		var cheshangbaovalue = $('#cheshangbao').data().value;
		cheshangbao.value(cheshangbaovalue);
	}
	//公告轮播

	var $this = $("#news");
	var scrollTimer;
	$this.hover(function() {
		clearInterval(scrollTimer);
	}, function() {
		scrollTimer = setInterval(function() {
			scrollNews($this);
		}, 2000);
	}).trigger("mouseleave");

	function scrollNews(obj) {
		var $self = obj.find("ul");
		var lineHeight = $self.find("li:first").height();
		$self.animate({
			"marginTop": -lineHeight + "px"
		}, 600, function() {
			$self.css({
				marginTop: 0
			}).find("li:first").appendTo($self);
		})
	}

	//固定标倒计时
	var timers=null;
	function timerFunc(intDiff) {
		if(timers){
			clearInterval(timers);
		}

		timers=setInterval(function() {
			var day = 0,
				hour = 0,
				minute = 0,
				second = 0; //时间默认值
			if(intDiff > 0) {
				day = Math.floor(intDiff / (60 * 60 * 24));
				hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
				minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
				second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
			}
			if(minute <= 9) minute = '0' + minute;
			if(second <= 9) second = '0' + second;
			$('#hour').html(hour);
			$('#min').html(minute);
			$('#sec').html(second);
			intDiff--;
			if(intDiff == -1) {
				$('#time').hide();
				$('#dotrailer').show();
			}
		}, 1000);
	}

	//新手标倒计时
	var newtimers=null;
	function newtimerFunc(intDiff) {
		if(newtimers){
			clearInterval(newtimers);
		}

		newtimers=setInterval(function() {
			var day = 0,
				hour = 0,
				minute = 0,
				second = 0; //时间默认值
			if(intDiff > 0) {
				day = Math.floor(intDiff / (60 * 60 * 24));
				hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
				minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
				second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
			}
			if(minute <= 9) minute = '0' + minute;
			if(second <= 9) second = '0' + second;
			$('#newhour').html(hour);
			$('#newmin').html(minute);
			$('#newsec').html(second);
			intDiff--;
			if(intDiff == -1) {
				$('#newtime').hide();
				$('#newdotrailer').show();
			}
		}, 1000);
	}

	//执行倒计时
	if($('#trailer').length != 0) {
		var countdownTime = $('#countdown').attr('data-time');
		timerFunc(parseInt(countdownTime)/1000);
	}
	//执行新手倒计时
	if($('#newMember').length != 0) {
		var newcountdownTime = $('#newcountdown').attr('data-time');
		newtimerFunc(parseInt(newcountdownTime)/1000);
	}

	$('.index_erweima_close').on('click', function () {
		$(this).parent().hide()
	});

	//立即下载
	$('#gotoDownload').click(function () {
		$('#appdetails').addClass('slideLeft');
		$('#appDownloadErweima').addClass('slideRight');
	});
	$('#appDownloadErweima').click(function () {

		$('#appdetails').removeClass('slideLeft');

		$('#appDownloadErweima').removeClass('slideRight');
	})
	$('.savemoney .left_part .left_btn p').bind('click',function(){
		$(this).addClass('cur').siblings().removeClass('cur');
		console.info(this.id);
		if(this.id =="cash"){
			getList({itemType:1},'/index/itemList');
		}else if(this.id =="mortgage"){
			getList({itemType:2},'/index/itemList');
		}else if(this.id =="carLoan"){
			getList({itemType:3},'/index/itemList');
		}else if(this.id =="debt"){
			getList({'days':0, 'rate': 0, 'amount': 0, 'itemRepayMethod':0,pageNum:1,pageSize:5},'/debt/investList');
		}
	});

	//存钱
	function getList(param,url){
		$.ajax({
			url :url,
			dataType : 'json',
			type : 'post',
			data:param,
			success : function(data) {
				var list='';
				if(data.list && data.list.length>0) {
					for (var i = 0; i < data.list.length; i++) {
						var itemAddRate = '';
						if (data.list[i].itemAddRate != 0) {
							itemAddRate = '<em class="percentage">+' + data.list[i].itemAddRate + '%</em>';
						}
						var itemCycleUnit = data.list[i].restPeriods + "<em style='font-size:16px'>天</em>";
						var addBtn = '';
						if (data.list[i].countdown > 0) {
							addBtn = '<div style="margin: 10px 0 !important;" class="re_r_countdown"><strong class="time countdown" data-time="' + data.list[i].countdown + '"><time class="hour">--</time>&nbsp;:<time class="min">--</time>&nbsp;:<time class="sec">--</time></strong><strong>开抢</strong></div>';
						} else {
							if (data.list[i].status == 0) {
								addBtn = '<div><a href="/debt/getDebtDetail/' + data.list[i].id + '?' + data.list[i].id + '"><input type="submit" value="立即投资"></a></div>';
							} else if (data.list[i].status == 1) {
								addBtn = '<div><a href="/debt/getDebtDetail/' + data.list[i].id + '?' + data.list[i].id + '"><input style="cursor: pointer;background: #c9c9c9" type="submit" value="已债转"></a></div>';
							} else if (data.list[i].status == 2) {
								addBtn = '<div><a href="/debt/getDebtDetail/' + data.list[i].id + '?' + data.list[i].id + '"><input style="cursor: pointer;background: #c9c9c9" type="submit" value="已过期"></a></div>';
							}
						}
						list += '<div class="right"><a href="/debt/getDebtDetail/' + data.list[i].id + '?' + data.list[i].id + '"><h2 class="itemNames" style="height: 57px">' + data.list[i].name + '</h2></a><div><div><p class="l">预期年化收益<span class="red">' + data.list[i].itemRate + '</span><em class="red">%</em>' + itemAddRate + '</p><p class="r" style="padding-top: 3px;height: 70px">剩余期限：<span class="day" style="color:#505050;font-size: 22px">' + itemCycleUnit + '</span></p></div><div><span class="l">转让金额<em style="margin-left: 10px">' + data.list[i].amount + '元</em></span></div><div class="plan_do"><p><span style="width:' + data.list[i].debtScale + '%"></span></p><em>' + data.list[i].debtScale + '%</em></div>' + addBtn + '</div></div>';
					}
					$("#pcItemList").html(list);
				}else if(!data.list && data.length>0){
					for(var i=0;i<data.length;i++){
						var itemAddRate='';
						if(data[i].itemAddRate !=0){
							itemAddRate='<em class="percentage">+'+data[i].itemAddRate+'%</em>';
						}
						var itemCycleUnit='';
						if(data[i].itemCycleUnit ==1){
							itemCycleUnit=data[i].itemCycle+"<em style='font-size:16px'>天</em>";
						}else if(data[i].itemCycleUnit ==2){
							itemCycleUnit=data[i].itemCycle+"<em style='font-size:16px'>月</em>";
						}else if(data[i].itemCycleUnit ==3){
							itemCycleUnit=data[i].itemCycle+"<em style='font-size:16px'>季</em>";
						}else if(data[i].itemCycleUnit ==4){
							itemCycleUnit=data[i].itemCycle+"<em style='font-size:16px'>年</em>";
						}
						var addBtn='';
						if(data[i].countdown >0){
							addBtn='<div style="margin: 10px 0 !important;" class="re_r_countdown"><strong class="time countdown" data-time="'+data[i].countdown+'"><time class="hour">--</time>&nbsp;:<time class="min">--</time>&nbsp;:<time class="sec">--</time></strong><strong>开抢</strong></div>';
						}else{
							if(data[i].itemStatus ==20){
								addBtn='<div><a href="/item/details/'+data[i].id+'?'+data[i].id+'"><input style="cursor: pointer;background: #c9c9c9" type="submit" value="已抢完"></a></div>';
							}else if(data[i].itemStatus ==30 || data[i].itemStatus ==31){
								addBtn='<div><a href="/item/details/'+data[i].id+'?'+data[i].id+'"><input style="cursor: pointer;background: #c9c9c9" type="submit" value="还款中"></a></div>';
							}else if(data[i].itemStatus ==32){
								addBtn='<div><a href="/item/details/'+data[i].id+'?'+data[i].id+'"><input style="cursor: pointer;background: #c9c9c9" type="submit" value="已还款"></a></div>';
							}else if(data[i].itemStatus ==23 || data[i].itemStatus ==18){
								addBtn='<div><a href="/item/details/'+data[i].id+'?'+data[i].id+'"><input style="cursor: pointer;background: #c9c9c9" type="submit" value="已满标"></a></div>';
							}else{
								addBtn='<div><a href="/item/details/'+data[i].id+'?'+data[i].id+'"><input type="submit" value="立即投资"></a></div>';
							}
						}
						list+='<div class="right"><a href="/item/details/'+data[i].id+'?'+data[i].id+'"><h2 class="itemNames" style="height: 57px">'+data[i].itemName+'</h2></a><div><div><p class="l">预期年化收益<span class="red">'+data[i].itemRate+'</span><em class="red">%</em>'+itemAddRate+'</p><p class="r" style="padding-top: 3px;height: 70px">投资期限：<span class="day" style="color:#505050;font-size: 22px">'+itemCycleUnit+'</span></p></div><div><span class="l">可投金额<em style="margin-left: 10px">'+(data[i].itemAccount-data[i].itemOngoingAccount)+'元</em></span></div><div class="plan_do"><p><span style="width:'+data[i].itemScale+'%"></span></p><em>'+data[i].itemScale+'%</em></div>'+addBtn+'</div></div>';
					}
					$("#pcItemList").html(list);
					$('.countdown').each(function () {
						var obj=$(this);
						var countdownTime=$(this).attr('data-time');
						getTimer(countdownTime/1000,obj);
					});
				}else{
					$("#pcItemList").html('');
				}
			},
			error : function() {
				layer.msg("服务器通讯失败", {
					icon: 5
				});
				layer.closeAll('loading');
			}
		});
	}
	getList({itemType:1},'/index/itemList');
	//倒计时
	function getTimer(intDiff,obj){
		if( obj.timers){
			clearInterval( obj.timers);
		}

		obj.timers=setInterval(function(){
			var day=0,
				hourc=0,
				minutec=0,
				secondc=0;//时间默认值
			if(intDiff > 0){
				day = Math.floor(intDiff / (60 * 60 * 24));
				hourc = Math.floor(intDiff / (60 * 60)) - (day * 24);
				minutec = Math.floor(intDiff / 60) - (day * 24 * 60) - (hourc * 60);
				secondc = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hourc * 60 * 60) - (minutec * 60);
			}

			if (minutec <= 9) minutec = '0' + minutec;
			if (secondc <= 9) secondc = '0' + secondc;
			obj.find('.hour').html(hourc);
			obj.find('.min').html(minutec);
			obj.find('.sec').html(secondc);
			intDiff--;
		}, 1000);
	}
	$('.countdown').each(function () {
		var obj=$(this);
		var countdownTime=$(this).attr('data-time');
		getTimer(countdownTime/1000,obj);
	});

	function threeNumber(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
			num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
			cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+
				num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + num + '.' + cents);
	}
	// $('.peopleUse').html(Number(threeNumber(parseInt($('.peopleUse').html()))))
	$('.todyMoney').html(threeNumber($('.todyMoney').html()))
	$('.getMoney').html(threeNumber($('.getMoney').html()))
	//合作伙伴轮播
	var coo_l = $('.companies .left');
	var coo_r = $('.companies .right');
	var coo_c = $('.cooperation ul');
	var con_time =0;
	coo_r.bind('click',function(){
		con_time++;
		if(con_time==6){
			coo_c.css({"left":0});
			con_time=0;
		}else{
			coo_c.css({"left":-183*con_time+"px"});
		}
	})
	coo_l.bind("click", function () {
		con_time--;
		console.log(con_time);
		if(con_time==-1){
			coo_c.css({"left":"-915px"});
			con_time=5;
		}else{
			coo_c.css({"left":-183*con_time+"px"});
		}
	})


	//判断是否登录
	$.post("/login/isLogin",function(data){
		if(data.result=='fail'){
           $('.register1').prop('href','/register')
		}else{
			$('.register1').prop('href','/user/assets')
		}
	})

});